package com.jet.multichat.server;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;

//TODO:[au] write a contract to the class
public class AccessManager {

	private String login;
	private BufferedReader threadReader;
	private PrintWriter threadWriter;

	public AccessManager(BufferedReader threadReader, PrintWriter threadWriter) {
		this.threadReader = threadReader;
		this.threadWriter = threadWriter;
	}
	
	//TODO:[au] write a contract to the method
	public boolean getAccess() {

		String login = null;
		String password = null;

		boolean access = false;
		
		try {
			threadWriter.println("server > Enter login");
			threadWriter.flush();			
			
			login = threadReader.readLine();
			
			threadWriter.println("server > Enter password");
			threadWriter.flush();

			password = threadReader.readLine();

			access = accessStub(login, password);
			
		} catch (IOException e) {
			//[ek] TODO will user know about error?
			e.printStackTrace();
		}
		return access;
	}

	private boolean accessStub(String login, String password) throws IOException {
		
		//[ek] TODO WTF?
		//stub
		if (login.equals(password)) {
			this.setLogin(login);
			
			return true;
		} else {
			threadWriter.println("server > wrong login-password entered. retype");
			threadWriter.flush();
		}

		return false;
	}

	public String getLogin() {
		return login;
	}

	public void setLogin(String login) {
		this.login = login;
	}
	
}
